package ThreadTest;
class Counter {
    private int count = 0;
    void add(){
        synchronized (this) {
            count++;
        }
    }
    int get(){
        return count;
    }
 }
public class Dome18 {
    public static void main(String[] args) throws InterruptedException{
        Counter c = new Counter();
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 50000; i++) {
                c.add();
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (c) {
                for (int i = 0; i < 50000; i++) {
                    c.add();
                }
            }

        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(c.get());
    }
}
